/*
 * gstcallback.h - Gstreamer callbacks
 *
 * Copyright (C) 2010-2011 Serge Begger
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef GSTCALLBACK_H
#define GSTCALLBACK_H

GstElement *dvbsrc, *d_audio, *d_video;
bool thisVideo;
bool thisAPE;
WId win;
static Display *display;

gboolean busCallback(GstBus*, GstMessage* msg, void *userdata)
{
    (void)userdata;
    if (GST_MESSAGE_TYPE(msg) == GST_MESSAGE_EOS) {
        g_print ("End stream");
    } else if (GST_MESSAGE_TYPE(msg) == GST_MESSAGE_STATE_CHANGED) {

    } else if (GST_MESSAGE_TYPE(msg) == GST_MESSAGE_ELEMENT) {
        if (gst_structure_has_name (msg->structure, "prepare-xwindow-id") && !win)
        {
            gst_x_overlay_set_xwindow_id (GST_X_OVERLAY(GST_MESSAGE_SRC(msg)), win);
            return GST_BUS_DROP;
        }
    } else if (GST_MESSAGE_TYPE(msg) == GST_MESSAGE_ERROR) {
        GError* error;
        gchar* debugs;

        g_print(error->message);
        g_print(debugs);

        g_error_free(error);
        free(debugs);
    }
    return GST_BUS_DROP;
}

static void findType (GstElement *typefind,
                      guint       probability,
                      GstCaps    *caps,
                      void    *userdata)
{
    (void)userdata;(void)typefind;(void)probability;
    gchar *type = gst_caps_to_string(caps);
    if (!strcmp(type ,"application/x-ape"))
        thisAPE = true;
    else
        thisAPE = false;
    if (!strcmp(type ,"video/x-matroska") ||
        !strcmp(type ,"video/x-msvideo") ||
        !strcmp(type ,"video/x-cdxa") ||
        !strcmp(type ,"application/x-3gp") ||
        !strcmp(type ,"video/x-ms-asf") ||
        !strncmp(type ,"video/mpegts", 12) ||
        !strncmp(type ,"video/mpeg", 10) ||
        !strncmp(type ,"audio/x-m4a", 11) ||
        !strcmp(type ,"video/quicktime"))
        thisVideo = true;
    else
        thisVideo = false;
    g_free (type);
}

static void on_pad_added (GstElement *element,
        GstPad *pad,
        gpointer data)
{
        GstCaps *caps;
        const gchar *mime = "NULL";
        (void) element;
        (void) data;
        guint i;

        GstPad *audiopad, *videopad;

        caps = gst_pad_get_caps (pad);
        g_assert (caps != NULL);
        for (i = 0; i < gst_caps_get_size (caps); ++i) {
                mime = gst_structure_get_name (gst_caps_get_structure (caps, i));
        }

        if (g_strrstr (mime, "audio")) {
                audiopad = gst_element_get_static_pad (d_audio, "sink");
                g_assert(audiopad);
                if (GST_PAD_IS_LINKED (audiopad)) {
                        g_object_unref (audiopad);
                        return;
                }
                gst_pad_link (pad, audiopad);
                gst_object_unref (audiopad);
        }

        if (g_strrstr (mime, "video")) {
                videopad = gst_element_get_static_pad (d_video, "sink");
                g_assert(videopad);
                if (GST_PAD_IS_LINKED (videopad)) {
                        g_object_unref (videopad);
                        return;
                }
                gst_pad_link (pad, videopad);
                gst_object_unref (videopad);
        }


        gst_caps_unref (caps);
}

GstElement *
make_queue ()
{
  GstElement *queue = gst_element_factory_make ("queue", NULL);
  g_object_set (queue,
          "max-size-time", (guint64) 0,
          "max-size-bytes", (guint64) 0,
          "max-size-buffers", (guint64) 0, NULL);

  return queue;
}

#endif // GSTCALLBACK_H
